<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
/*展位控制器模板*/
class StandController extends AdminbaseController{
	protected $model_model;
	protected $ad_model;
	protected $constants;
	
	function _initialize() {
		parent::_initialize();
		$this->model_model = D("Common/Stand");
		$this->ad_model= D("Common/Ad");
		$this->constants= D("Common/Constant");
	}
	
	function index(){
		$this->_list();
		$this->display();
	}
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			if ($this->model_model->create()){
				if ($this->model_model->add()!==false) {
					$this->success(L('ADD_SUCCESS'), U("Stand/index"));
				} else {
					$this->error(L('ADD_FAILED'));
				}
			} else {
				$this->error($this->model_model->getError());
			}
		
		}
	}
	
	function edit(){
		$id=I("get.id");
		$model=$this->model_model->where("id=$id")->find();
		$this->assign('model',$model);
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			if ($this->model_model->create()) {
				if ($this->model_model->save()!==false) {
					$this->success("保存成功！", U("Stand/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->model_model->getError());
			}
		}
	}
	
	/*广告列表*/
	function ad_index(){
		$this->_ad_list();
		$this->display();
	}
	
    /*发布信息列表*/
	function publish_index(){
		$this->_publish_list();
		$this->display();
	}
	
	
	/*发布审批*/
	function publish_detail(){
		$model=M("publish")->find(I("id",'0'));
		$this->assign("model",$model);
		$this->display();
	}
	
		/*发布审核*/
	function publish_audit(){
		$reason=I("reason",'');
		$opt_type=I("opt_type");
		$id=I("id");
		if($opt_type=='yes'){//审批通过
			M("publish")->where("id=$id")->setField("status",'3');
		}else{
			M("publish")->where("id=$id")->setField(array("status"=>'2','mark'=>$reason));
		}
		$this->success("success",U("Stand/publish_index"));
	}
	
	function publish_delete(){
		$id = I("get.id",0,"intval");
		if (M("publish")->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	
	
	}
	/*广告审批*/
	function audit(){
		$reason=I("reason",'');
		$opt_type=I("opt_type");
		$id=I("id");
		if($opt_type=='yes'){//审批通过
			$this->ad_model->where("id=$id")->setField("status",'3');
		}else{
			$this->ad_model->where("id=$id")->setField(array("status"=>'2','mark'=>$reason));
		}
		$this->success("success",U("Stand/ad_index"));
	}
	
	/*广告审批*/
	function ad_detail(){
		$model=$this->ad_model->find(I("id",'0'));
		$this->assign("adTypes",$this->constants->getAdTypeKV());// 广告类型
		$this->assign("areas",$this->constants->getAreaKV());// 地区类型
		$this->assign("model",$model);
		$this->assign("imgs",M("ImgLib")->where("ad_id=$model[id]")->select());
		$this->display();
	}
	/*删除广告*/
	function ad_delete(){
		$id = I("get.id",0,"intval");
		if ($this->ad_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	
	}
	/**
	 *  删除展位
	 */
	function delete(){
		$id = I("get.id",0,"intval");
		$count=$this->ad_model->where("stand_id=$id")->count(1);
		if($count>0){
			$this->error("请先删除展位下的广告！");
		}
		if ($this->model_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	function toggle(){
		if(isset($_POST['ids']) && $_GET["display"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=1;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("显示成功！");
			} else {
				$this->error("显示失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["hide"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=0;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("隐藏成功！");
			} else {
				$this->error("隐藏失败！");
			}
		}
	}
	///////////////////////////////////////////////////////////////////////////
		private function _list() {
	    $fields = array('a_is_roll'=>array("field" =>"a.is_roll","operator" =>"like"));
	    $where_and = $this ->get_where_ands(IS_POST, $fields);
	    $where = join(' and ', $where_and);
		//过滤条件  审批通过且 没有到期显示  
		$map=" and b.status=2 and b.expire_date>".date('Y-m-d');
	    $count = $this ->model_model ->alias("a")->join("left join m_ad as b on a.id=b.stand_id $map ") ->where($where) ->count(1);
	
	    $page = $this ->page($count, 20);
	    $this ->assign("Page", $page ->show('Admin'));
	    $this ->assign("current_page", $page ->GetCurrentPage());
		$field_str="a.id,a.change_day,a.listorder,a.create_time,a.is_roll,b.link_name";
	    $list = $this ->model_model ->alias("a")->field($field_str)->join("left join m_ad as b on a.id=b.stand_id $map")->where($where)->order('a.create_time desc') ->limit($page ->firstRow.','.$page ->listRows) ->select();
	    $this ->assign('list', $list);
	}
		
		/*待审批的广告列表*/
		private function _ad_list() {
	    $fields = array('a_link_name'=>array("field" =>"a.link_name","operator" =>"like"),
						'a_type_id'=>array("field" =>"a.type_id","operator" =>"like"),
						'a_status'=>array("field" =>"a.status","operator" =>"like"));
	    $where_and = $this ->get_where_ands(IS_POST, $fields);
	    $where = join(' and ', $where_and);
	    $count = $this ->ad_model ->alias("a")->join("left join m_stand as b on b.id=a.stand_id") ->where($where) ->count(1);
	
	    $page = $this ->page($count, 20);
	    $this ->assign("Page", $page ->show('Admin'));
	    $this ->assign("current_page", $page ->GetCurrentPage());
		$field_str="a.id,a.contact,a.mobile,a.user_uniq_id,a.type_id,a.province_id,a.city_id,a.click_count,a.status,a.expire_date,a.link_url,a.link_name,a.mark,a.discount_rate,a.discount_name,a.stand_id,a.create_time,a.spend,b.is_roll";
	    $list = $this ->ad_model ->alias("a")->field($field_str)->join("left join m_stand as b on b.id=a.stand_id")->where($where)->order(' a.status asc,a.create_time desc') ->limit($page ->firstRow.','.$page ->listRows) ->select();
		
	    $this ->assign('list', $list);
		$this->assign("adTypes",$this->constants->getAdTypeKV());// 广告类型
		$this->assign("areas",$this->constants->getAreaKV());// 地区类型
	}	

	/*publish列表*/
		private function _publish_list() {
	    $fields = array('a_title'=>array("field" =>"a.title","operator" =>"like"),
						'a_status'=>array("field" =>"a.status","operator" =>"like"));
	    $where_and = $this ->get_where_ands(IS_POST, $fields);
	    $where = join(' and ', $where_and);
	    $count = M("Publish") ->alias("a")->where($where) ->count(1);
	
	    $page = $this ->page($count, 20);
	    $this ->assign("Page", $page ->show('Admin'));
	    $this ->assign("current_page", $page ->GetCurrentPage());
	    $list =M("Publish") ->alias("a")->where($where)->order(' a.status asc,a.create_time desc') ->limit($page ->firstRow.','.$page ->listRows) ->select();
		
	    $this ->assign('list', $list);
		$this->assign("adTypes",$this->constants->getAdTypeKV());// 广告类型
		$this->assign("areas",$this->constants->getAreaKV());// 地区类型
	}	
}